/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trim.gui.testing;

import java.awt.BorderLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import trim.Trim;
import trim.common.inputdata.InputData;
import trim.common.inputdata.InputDataException;
import trim.common.result.TrimResult;

/**
 * 
 * Created 28.12.2008 15:42:53
 * @author Саковнич Дмитрий
 */
public class TestsRunner
{

  List<InputData> lInputData;
  List<Trim> methods;
  List<TestRunnerListener> listeners=new ArrayList();
  InputData currenInputData=null;
  private static boolean testing=false;

  public TestsRunner(List<InputData> lInputData,List<Trim> methods)
  {
    this.lInputData=lInputData;
    this.methods=methods;
  }

  public void addListener(TestRunnerListener trl)
  {
    listeners.add(trl);
  }

  public void removeListener(TestRunnerListener trl)
  {
    listeners.remove(trl);
  }

  public void startTests()
  {
    if(testing)
      return;
    JDialog dialog=new JDialog((Window)null);
    dialog.setLocation(500,500);
    dialog.setSize(500,100);
    dialog.setLayout(new BorderLayout());
    JLabel label=new JLabel("");
    dialog.add(label,BorderLayout.NORTH);
    JButton bt=new JButton("Stop test");
    dialog.add(bt,BorderLayout.SOUTH);
    final Thread t=new TestsThread(label);
    bt.addActionListener(new ActionListener()
    {

      @Override
      public void actionPerformed(ActionEvent e)
      {
        t.stop();
        testing=false;
        try
        {
          currenInputData.saveDataToXML(AppState.getProgrammDir()+File.separator+"lasttest.xml");
        }catch(InputDataException ex)
        {
          ex.printStackTrace();
        }
        ((JButton)e.getSource()).getRootPane().getParent().setVisible(false);
      }
    });
    testing=true;
    dialog.setModal(false);
    dialog.setAlwaysOnTop(true);
    dialog.setVisible(true);
    t.start();
  }

  private void fireNewResult(InputData inputData,TrimResult tr,Trim method,long time)
  {
    for(TestRunnerListener trl:listeners)
      trl.newResultEvent(inputData,tr,method,time);
  }

  private class TestsThread extends Thread
  {

    JLabel label;
    JDialog dialog;

    public TestsThread(JLabel label/*,JDialog dialog*/)
    {
      this.label=label;
    //this.dialog=dialog;
    }

    //List<InputData> lInputData;
    //List<Trim> methods;
    @Override
    public void run()
    {
      int k=0;
      SimpleDateFormat sdf=new SimpleDateFormat("HH:mm:ss.S");
      for(InputData d:lInputData)
      {
        k++;
        currenInputData=d;
        for(Trim t:methods)
        {
          synchronized(label)
          {

            label.setText("Test "+k+"/"+lInputData.size()+" method-"+t.getName()+" "+
              "started at "+sdf.format(new Date()));
          }
          try
          {
            long start=System.currentTimeMillis();
            TrimResult tr=t.solve(d);
            long end=System.currentTimeMillis();
            fireNewResult(d,tr,t,end-start);

          }catch(Exception ex)
          {
            ex.printStackTrace();
          }
        }
      }
      label.getRootPane().getParent().setVisible(false);
      testing=false;
    }
  }
}
